// 在node环境中运行
const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const VueLoaderPlugin = require('vue-loader/lib/plugin')

module.exports = {
  entry: './src/main.js',
  mode: 'none',
  output: {
    filename: 'bundle.js',
    path: path.join(__dirname, 'dist')
    // publicPath: 'dist/'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: '/node_modules',
        use: {
          loader: 'babel-loader',
          options: {
            presets: [
              ['@babel/preset-env', { modules: false }],
              ['@vue/babel-preset-app']
            ]
          }
        }
      },
      {
        test: /\.css$/,
        exclude: '/node_modules',
        use: [
          {
            loader: 'style-loader'
          },
          {
            loader: 'css-loader'
          }
        ]
      },
      {
        test: /\.less$/,
        exclude: '/node_modules',
        use: [
          {
            loader: 'style-loader'
          },
          {
            loader: 'css-loader'
          },
          {
            loader: 'less-loader'
          }
        ]
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        exclude: '/node_modules',
        use: {
          loader: 'url-loader',
          options: {
            limit: 10 * 1024 // 10KB
          }
        }
      },
      {
        test: /\.html$/,
        exclude: '/node_modules',
        use: {
          loader: 'html-loader',
          options: {
            attributes: {
              list: [
                {
                  tag: 'img',
                  attribute: 'src',
                  type: 'src'
                },
                {
                  tag: 'img',
                  attribute: 'srcset',
                  type: 'srcset'
                },
                {
                  tag: 'img',
                  attribute: 'data-src',
                  type: 'src'
                },
                {
                  tag: 'img',
                  attribute: 'data-srcset',
                  type: 'srcset'
                },
                {
                  tag: 'a',
                  attribute: 'href',
                  type: 'src'
                },
                {
                  tag: 'link',
                  attribute: 'href',
                  type: 'src',
                  filter: (tag, attribute, attributes) => {
                    if (!/stylesheet/i.test(attributes.rel)) {
                      return false
                    }

                    if (
                      attributes.type &&
                                        attributes.type.trim().toLowerCase() !== 'text/css'
                    ) {
                      return false
                    }

                    return true
                  }
                }
              ]
            }
          }
        }
      },
      {
        test: /\.vue$/,
        use: 'vue-loader'
      }
    ]
  },
  plugins: [
    new VueLoaderPlugin(),
    new HtmlWebpackPlugin({
      title: 'Webpack Plugin Sample',
      filename: 'index.html',
      meta: {
        viewport: 'width=device-width'
      },
      template: './public/index.ejs',
      force: true
    }),
    new webpack.DefinePlugin({
      API_BASE_URL: JSON.stringify('https://api.example.com'),
      BASE_URL: JSON.stringify('https://example.com')
    })
  ]
}
